package com.itgaoshu.hospitalproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.*;
import com.itgaoshu.hospitalproject.service.LadminService;
import com.itgaoshu.hospitalproject.service.LeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("liao")
public class LadminController {
    @Autowired
    private LadminService ladminService;
    //跳转到入院登记页面
    @RequestMapping("admin")
    public String admin(){
        return "liao/admin";
    }
    //查询未出院的所有的患者
    @RequestMapping("selRegister")
    @ResponseBody
    public Object selRegister(Register register,Integer page,Integer limit){
        //分页查询
        PageHelper.startPage(page,limit);
        //查询未出院的所有患者信息
        List<Register> registers = ladminService.selRegister(register);
        //将查到的信息放入到分页插件中
        PageInfo pageInfo = new PageInfo(registers);
        Map<String,Object> tableDate = new HashMap<>();
        //根据layUI将Jason数据返回
        tableDate.put("code","0");
        tableDate.put("msg","");
        //将全部的条数作为count传给前台(一共多少条)
        tableDate.put("count",pageInfo.getTotal());
        //将对象也封装到该map集合中
        tableDate.put("data",pageInfo.getList());
        //将数据返回到layUI中
        return tableDate;
    }

    //查询所有的科室信息
    @RequestMapping("selDepartment")
    @ResponseBody
    public Object selDepartment(){
        List<Departments> departments = ladminService.selDepartment();
        return departments;
    }

    //根据科室查询该科室下的医生以及属于住院部的dstate=1
    @ResponseBody
    @RequestMapping("selDoctor")
    public Object selDoctor(Integer departmentId){
        List<Doctor> doctors = ladminService.selDoctor(departmentId);
        return doctors;
    }

    //根据科室查询没有入住的床位信息
    @RequestMapping("selBed")
    @ResponseBody
    public Object selBed(Integer departmentId){
        Bed bed = new Bed();
        bed.setDepartmentId(departmentId);
        bed.setState(0);
        List<Bed> beds = ladminService.selBed(bed);
        return beds;
    }

    //查询医保折扣的信息的方法
    @RequestMapping("selDis")
    @ResponseBody
    public Object selDis(){
        List<Moneytype> moneytypes = ladminService.selDis();
        return moneytypes;
    }

    //查询待转院的所有患者的信息从门诊传过来的信息
    @ResponseBody
    @RequestMapping("selDoor")
    public Object selDoor(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Register> reportVos = ladminService.selDoor();
        PageInfo pageInfo = new PageInfo(reportVos);
        Map<String,Object> tableDate = new HashMap<>();
        tableDate.put("code",0);
        tableDate.put("msg","");
        tableDate.put("count",pageInfo.getTotal());
        tableDate.put("data",pageInfo.getList());
        return tableDate;
    }

    //添加住院的患者的信息
    @RequestMapping("addRegister")
    @ResponseBody
    public String addRegister(Register register, HttpServletRequest request){
        //获取用户的姓名在session中
        String realname = (String)request.getSession().getAttribute("yonghu");
        register.setOperator(realname);
        //根据患者的身份证来查询是否有这个患者办理入住
        Register register1 = new Register();
        register1.setIdcard(register.getIdcard());
        List<Register> registers = ladminService.selRegister(register1);
        if(registers.size()==0){//说明没有这个患者，可以进行添加
            //判断是否是从门诊转过来的信息，如果是门诊转过来的患者，将门诊部门中的待入院状态改为已入院状态即可
            if(register.getReportid()!=0){
                //改为入院的状态
                ladminService.updateZ(register);
            }
            //添加患者信息
            int i = ladminService.addRegister(register);
            if(i==1){//表名入院成功
                //修改床位的信息
                int i1 = ladminService.updateBed(register);
                if(i1==1){
                    //表明修改成功
                    return "修改成功";
                }else{
                    return "修改失败";
                }
            }else{
                return "入院失败";
            }
        }else{
            return "已经有该患者了，不能在进行重复添加了";
        }
    }


    //转科室的方法

    @Autowired
    private LeaveService leaveService;

    /**
     * 转科室需要三步操作：
     * 1.需要将原来科室的病床改成未被占用
     * 2.将新的科室的病床改成已占用
     * 3.修改患者所属的科室
     * @param register
     * @return
     */
    @ResponseBody
    @RequestMapping("updKe")
    public Object updKe(Register register){
        //将原有的病床改为空床
        leaveService.updateBed(register);
        //将现在的科室的病床改成被占用的状态
        ladminService.updateBed(register);
        //修改患者所属的科室
        int i = ladminService.updateKe(register);
        if(i==1){
            return "更改成功";
        }else{
            return "更改失败";
        }
    }


}
